<?php
/**
 * User: hjd-yao
 * Date: 2019/6/12
 * Time: 17:34
 */

namespace Shop\Http\Controllers\Wap;


use App\Helpers\SelfResponse;
use App\Models\Shop\AgentSalesModel;
use App\Models\User\AccountCorrectModel;
use App\Models\User\AccountFlowModel;
use Carbon\Carbon;
use DC\UserCenter\Models\UserWalletModel;
use Frontend\Models\UserBaseModel;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Shop\Http\Controllers\BaseController;

class AgentController extends BaseController
{
    public function agentSales(Request $request)
    {
        $user_id = $request->input('user_id');
        $wallet = UserWalletModel::find($user_id);
        return $this->wapRender('agent.agent_sales',[
            'page_title'=>'代售',
            'wallet'=>$wallet
        ]);
    }

    /**
     * @title 消费积分代售
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function xfAgent(Request $request)
    {
        $user_id = $request->input('user_id');
        $wallet = UserWalletModel::find($user_id);
        $is_open = AgentSalesModel::init()->isOpen();
        if($request->isMethod('post')){
            if(!$is_open){
                return SelfResponse::error('暂未开放');
            }
            $agent_num  = AgentSalesModel::init()->hasSales($user_id);
            if($agent_num){
                $tnum = UserBaseModel::where('referrer_id',$user_id)->where('status','>=',0)->count();
                if($tnum<9){
                    return SelfResponse::error('一天仅限代售一次');
                }elseif($agent_num>=2){
                    return SelfResponse::error('一天仅限代售两次');
                }
            }

            if(!UserWalletModel::init()
                ->compareSurplus(get_rule('gw_agent_need_score'),$user_id,'money2')
            ){
                return SelfResponse::error(get_currency()['money2'].'不足');
            }
            $need_money = abs(get_rule('gw_agent_need_score'));
            if(UserWalletModel::where('member_id',$user_id)->decrement('money2',$need_money)){
                $id = AgentSalesModel::init()->createXfAgent($user_id);
                $params = [
                    'money_type'=>3,
                    'change_type'=>46,
                    'before_money'=>$wallet->money2,
                ];
                AccountFlowModel::init()->createFlow($user_id,$need_money,0,$params);
                return SelfResponse::success('代售成功');
            }

            return SelfResponse::error('代售失败,请重新尝试');
        }else{

            return $this->wapRender('agent.agent_xf',[
                'page_title'=>get_currency()['money2'].'代售',
                'wallet'=>$wallet,
                'is_open'=>$is_open
            ]);
        }
    }

    /**
     * @title 财富金豆代售
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function dhAgent(Request $request)
    {
        $user_id = $request->input('user_id');
        $wallet = UserWalletModel::find($user_id);
        $is_open = AgentSalesModel::init()->isOpen('dh_agent_is_open');
        if($request->isMethod('post')){
            if(!$is_open){
                return SelfResponse::error('暂未开放');
            }

            if(AgentSalesModel::init()->hasSales($user_id,2)){
                return SelfResponse::error('一天仅限代售一次');
            }

            $need_money = abs(get_rule('dh_agent_need_score'));
            if($wallet->money2-$need_money<0){
                return SelfResponse::error(get_currency()['money2'].'不足');
            }

            if($wallet->money1-$need_money<0){
                return SelfResponse::error(get_currency()['money1'].'不足');
            }

            $number = floor(min($wallet->money2,$wallet->money1)/$need_money);
            if(!$number){
                return SelfResponse::error('余额不足');
            }

            $pay_money = $number*$need_money;
            $data['money2'] = DB::raw('`money2`-'.$pay_money);
            $data['money1'] = DB::raw('`money1`-'.$pay_money);
            if(UserWalletModel::where('member_id',$user_id)->update($data)){
                $id = AgentSalesModel::init()->createDhAgent($user_id,$number);
                $params = [
                    'money_type'=>1,
                    'change_type'=>47,
                    'before_money'=>$wallet->money1,
                ];
                AccountFlowModel::init()->createFlow($user_id,$pay_money,0,$params);

                $params = [
                    'money_type'=>4,
                    'change_type'=>47,
                    'before_money'=>$wallet->money2,
                ];
                AccountFlowModel::init()->createFlow($user_id,$pay_money,0,$params);

                return SelfResponse::success('代售成功');
            }

        }else{
            return $this->wapRender('agent.agent_dh',[
                'page_title'=>get_currency()['money2'].'代售',
                'wallet'=>$wallet,
                'is_open'=>$is_open
            ]);
        }
    }

    public function myAgent(Request $request)
    {
        $user_id = $request->input('user_id');
        $pageNum = $request->input('page_num',1);
        $agent_type = $request->input('type',1);
        $list = AgentSalesModel::where('user_id',$user_id)->where('agent_type',$agent_type)
            ->forPage($pageNum,10)->get();
        return $this->wapRender('agent.agent_me',[
            'page_title'=>'我的代售',
            'list'=>$list,
            'agent_type'=>$agent_type
        ]);
    }
}